<?php

namespace __TARGET_BUNDLE__\Controller\AdminCommon;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use __TARGET_BUNDLE__\Base\BaseController;
use __TARGET_BUNDLE__\Entity\__TARGET_ENTITY__ as Entity;
use __TARGET_BUNDLE__\Setting\__TARGET_ENTITY__Setting;

/**
 * @route("/admin/")
 */
class __TARGET_ENTITY__CommonController extends BaseController
{

    /**
     * @route("__TARGET_ENTITY__/add",name="__TARGET_ENTITY__PreAdd")
     */
    public function add()
    {
        //私有代码
        if ( $parent = $this->getParamRequired("parent")) {
            $entity = new Entity();
            $entity->setParent($parent);
        }else{
            $entity = null;
        }
        //通用代码
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        return $this->comAdd($entity);
    }

    /**
     * @route("__TARGET_ENTITY__/add/do",name="__TARGET_ENTITY__DoAdd")
     *
     *
     */
    public function doAdd(Request $request)
    {
        //私有代码
        $entity = new Entity();
        $parent = $this->getParamRequired("form");
        $rep = $this->getDoctrine()->getRepository("__TARGET_BUNDLE__:__TARGET_ENTITY__");
        $parentEntity = $rep->find($parent["parent"]);
        if ($parentEntity) {
            $level = $parentEntity->getLevel() + 1;
        } else {
            $level = 0;
        }
        $entity->setLevel($level);

        //通用代码
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        return $this->comDoAdd($request,$entity);
    }

    /**
     * @route("__TARGET_ENTITY__/edit/{id}",name="__TARGET_ENTITY__PreEdit")
     */
    public function edit(Entity $entity)
    {
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        return $this->comEdit($entity);
    }

    /**
     * @route("__TARGET_ENTITY__/edit/{id}/do",name="__TARGET_ENTITY__DoEdit")
     *
     *
     */
    public function doEdit(Request $request,Entity $entity)
    {
        //私有代码
        $parent = $request->get("form");
        $rep = $this->getDoctrine()->getRepository("__TARGET_BUNDLE__:__TARGET_ENTITY__");
        $parentEntity = $rep->find($parent["parent"]);
        if ($parentEntity) {
            $level = $parentEntity->getLevel() + 1;
        } else {
            $level = 0;
        }
        $entity->setLevel($level);

        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        return $this->comDoEdit($request,$entity);
    }

    /**
     * @route("__TARGET_ENTITY__",name="__TARGET_ENTITY__Lists")
     */
    public function lists()
    {
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        return $this->comCatLists();
    }


    /**
     * @route("__TARGET_ENTITY__/tools",name="__TARGET_ENTITY__Tools")
     */
    public function tools(Request $request)
    {
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        return $this->comTools($request);
    }

}
